// import DataBus from '../libs/data.js'; // 导入参数
export default {
	canvas:GameGlobal.databus.canvas,
	libsImage:GameGlobal.databus.image,
	ctx : GameGlobal.databus.ctx,
	// 触摸事件处理
	handleTouch(e) {
		const touch = e.touches[0];
		const x = touch.clientX;
		const y = touch.clientY;
		this.checkClick(x, y);
	},

	// 首页点击
	checkClick(x, y) {
		let canvas = GameGlobal.databus.canvas
		let btn1Bg = GameGlobal.databus.image.btn1Bg
		let rukouBg = GameGlobal.databus.image.rukouBg
		let ctx = GameGlobal.databus.ctx
		const kaishibtn = {
			x: canvas.width / 2 - btn1Bg.box.x,
			y: canvas.height / 2 - btn1Bg.box.y,
			w: btn1Bg.box.w,
			h: btn1Bg.box.h
		}
		// 点击开始游戏
		if (this.isInRect(x, y, kaishibtn)) {
			if (GameGlobal.databus.gamethis) {
				GameGlobal.databus.gamethis.render()
			} else {
				const loadTask = tt.loadSubpackage({
					name: 'stage1', // 填写 game.json 中 subPackages 填写的分包名称 name
					success: function(res) {
					},
					fail: function(res) {
						console.log("加载失败", ctx)
					}
				});
				loadTask.onProgressUpdate(res => {
					// console.log('下载进度', res.progress);
					// console.log('已经下载的数据长度', res.totalBytesWritten);
					// console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite);
				});
			}
		} else
			// 点击入口
			if (this.isInRect(x, y, rukouBg.box)) {
				// 是否触摸入口按钮
				tt.checkScene({
					scene: "sidebar",
					success: (res) => {
						console.log("check scene success: ", res.isExist);
						if (res.isExist) {
							tt.navigateToScene({
								scene: 'sidebar',
								fail: console.log,
								success: console.log
							})
						}
						//成功回调逻辑
					},
					fail: (res) => {
						console.log("check scene fail:", res);
						//失败回调逻辑
					}
				});
			}
	},


	// 辅助函数
	isInRect(x, y, rect) {
		return x >= rect.x && x <= rect.x + rect.w &&
			y >= rect.y && y <= rect.y + rect.h;
	},

	isInCircle(x, y, circle) {
		const dx = x - circle.x;
		const dy = y - circle.y;
		return dx * dx + dy * dy <= circle.r * circle.r;
	}
}